package com.ccf.business.etl.controller;

import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.common.utils.constant.Constants;
import com.common.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.common.model.pojo.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import com.ccf.business.etl.model.RuleDict;
import com.ccf.business.etl.service.IRuleDictService;
import com.common.model.result.PageResult;
import com.common.model.result.Result;

/**
 * 字典规范管理
 *
 * @author zhaoj
 * @date 2021-11-29 10:41:10
 */
@Slf4j
@RestController
@RequestMapping("/ruledict")
@Api(tags = "字典规范管理")
public class RuleDictController {
    @Autowired
    private IRuleDictService ruleDictService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表")
    @GetMapping
    public PageResult list(Query query) {
        return ruleDictService.findList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        RuleDict model = ruleDictService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody RuleDict ruleDict) {
        if (ruleDictService.isExistField(ruleDict.getId(), ruleDict.getRuleName(),
                "rule_name")){
            return Result.failed("规则名称已存在");
        }
        ruleDict.setCreator(UserInfo.DEFAULT_USER_ID);
        ruleDictService.saveOrUpdate(ruleDict);
        return Result.succeed("保存成功");
    }
    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        if(StrUtil.isEmpty(id)) {
            return Result.failed("请选择数据");
        }
        return Result.status(ruleDictService.removeByIds(Arrays.asList(StrUtil.split(id, Constants.COMMA))),"","删除成功", "删除失败");
    }

    /**
     * 删除校验
     */
    @ApiOperation(value = "删除校验")
    @GetMapping("/{id}/validate")
    public Result deleteValidate(@PathVariable String id) {
        if(StrUtil.isEmpty(id)) {
            return Result.failed("请选择数据");
        }
        return Result.succeed(ruleDictService.deleteValidate(id));
    }
}
